﻿Imports System
Imports System.Globalization
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Reflection
Imports Microsoft.SqlServer.Management.IntegrationServices
Imports Microsoft.SqlServer.Management.Sdk.Sfc
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common.SqlConnectionInfo
Imports LPExecutePackageTask
Imports System.Data.SqlClient

Partial Public Class LPExecutePackageTaskForm
    Inherits Form

    Private taskHostValue As TaskHost
    Private bStarting As Boolean
    Private sc As SSISCatalog

    Public Sub New(ByVal taskHost As TaskHost)

        Dim pkg As Microsoft.SqlServer.Management.IntegrationServices.PackageInfo

        bStarting = True

        InitializeComponent()

        With Me
            .taskHostValue = taskHost
            .txtTaskName.Text = taskHostValue.Name
            .txtTaskDescription.Text = taskHostValue.Description
            Dim sServerName As String = CStr(.taskHostValue.Properties("ServerName").GetValue(.taskHostValue))
            .txtServer.Text = sServerName
            .txtCatalog.Text = CStr(.taskHostValue.Properties("PackageCatalog").GetValue(.taskHostValue))
            .txtFolder.Text = CStr(.taskHostValue.Properties("PackageFolder").GetValue(.taskHostValue))
            .txtProject.Text = CStr(.taskHostValue.Properties("PackageProject").GetValue(.taskHostValue))
            .txtPackage.Text = CStr(.taskHostValue.Properties("PackageName").GetValue(.taskHostValue))

            LoadListPage()

            .btnDone.Left = 298

        End With

    End Sub

    Private Sub btnDone_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDone.Click

        With Me
            .taskHostValue.Name = .txtTaskName.Text
            .taskHostValue.Description = .txtTaskDescription.Text

            .taskHostValue.Properties("ServerName").SetValue(taskHostValue, CStr(txtServer.Text))
            .taskHostValue.Properties("PackageCatalog").SetValue(taskHostValue, CStr(txtCatalog.Text))
            .taskHostValue.Properties("PackageFolder").SetValue(taskHostValue, CStr(txtFolder.Text))
            .taskHostValue.Properties("PackageProject").SetValue(taskHostValue, CStr(txtProject.Text))
            .taskHostValue.Properties("PackageName").SetValue(.taskHostValue, CStr(txtPackage.Text))


        End With

    End Sub

    Private Sub LoadListPage()

        With Me

            .listPage.Items.Clear()
            .listPage.Items.Add("Catalog")
            .listPage.Items.Add("General")
            .listPage.SelectedIndex = 0

        End With

    End Sub

    Function getControlFromName(ByRef containerObj As Object, _
                          ByVal name As String) As Control

        Dim tempCtrl As Control

        Try
            For Each tempCtrl In containerObj.Controls
                If tempCtrl.Name.ToUpper.Trim = name.ToUpper.Trim Then
                    Return tempCtrl
                End If
            Next tempCtrl
        Catch ex As Exception
            Return Nothing
        End Try

    End Function

    Sub ValidatePackage()

        Dim sMsg As String = ""
        Dim pkg As Microsoft.SqlServer.Management.IntegrationServices.PackageInfo

        With Me
            Try

                If .txtServer.Text = "" Then
                    sMsg = "Please enter an SSIS Catalog server. "
                    .txtServer.Focus()
                End If

                If .txtCatalog.Text = "" Then
                    sMsg = sMsg & "Please enter an SSIS Catalog name. "
                    .txtCatalog.Focus()
                End If

                If .txtFolder.Text = "" Then
                    sMsg = sMsg & "Please enter an SSIS Folder name. "
                    .txtFolder.Focus()
                End If

                If .txtProject.Text = "" Then
                    sMsg = sMsg & "Please enter an SSIS Project name. "
                    .txtProject.Focus()
                End If

                If .txtPackage.Text = "" Then
                    sMsg = sMsg & "Please enter an SSIS Package name. "
                    .txtPackage.Focus()
                End If

                If sMsg = "" Then
                    pkg = returnPackageObject(.txtServer.Text, _
                                              .txtCatalog.Text, _
                                              .txtFolder.Text, _
                                              .txtProject.Text, _
                                              .txtPackage.Text)
                End If

            Catch ex As Exception
                'txtStatus.Text = "Issue loading package" & vbCrLf & ex.Message
            End Try

        End With

    End Sub

    Private Function returnPackageObject(server As String, _
                                         catalog As String, _
                                         folder As String, _
                                         project As String, _
                                         package As String) As Microsoft.SqlServer.Management.IntegrationServices.PackageInfo

        Try
            Dim oServer As New Server(server)
            Dim oIs As New IntegrationServices(oServer)
            Dim oCat As Catalog = oIs.Catalogs(catalog)
            Dim oFld As CatalogFolder = oCat.Folders(folder)
            Dim oPrj As ProjectInfo = oFld.Projects(project)
            Dim pkg As Microsoft.SqlServer.Management.IntegrationServices.PackageInfo = oPrj.Packages(package)

            Return pkg
        Catch ex As Exception
            Return Nothing
        End Try

    End Function

    Private Sub listPage_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listPage.SelectedIndexChanged

        FormSet(Me.listPage.SelectedIndex)

    End Sub

    Private Sub LPExecutePackageTaskForm_Resize(sender As Object, e As EventArgs) Handles Me.Resize

        FormResize(Me.listPage.SelectedIndex)

    End Sub

    Sub FormResize(iParamcount As Integer)

        With Me
            If Not bStarting Then
                .listPage.Height = .Height - 114

                .btnDone.Left = .Width - (btnDone.Width + 28)
                .btnDone.Top = .Height - (.btnDone.Height + 55)

                .gbGeneral.Left = .listPage.Left + .listPage.Width + 10
                .gbGeneral.Top = .listPage.Top
                .gbGeneral.Width = .Width - (.listPage.Left + .listPage.Width + 40)
                .gbGeneral.Height = .listPage.Height
                .txtTaskName.Width = .gbGeneral.Width - 97
                .txtTaskDescription.Width = .txtTaskName.Width

                .gbPackageConfig.Left = .listPage.Left + .listPage.Width + 10
                .gbPackageConfig.Top = .listPage.Top
                .gbPackageConfig.Width = .Width - (.listPage.Left + .listPage.Width + 40)
                .gbPackageConfig.Height = .listPage.Height
                .txtServer.Width = .gbPackageConfig.Width - 124
                .btnDisplayCatNav.Left = .txtServer.Left + .txtServer.Width + 2
                .txtCatalog.Width = .gbPackageConfig.Width - 89
                .txtFolder.Width = .txtCatalog.Width
                .txtProject.Width = .txtCatalog.Width
                .txtPackage.Width = .txtCatalog.Width

            End If
        End With

    End Sub

    Sub FormSet(i As Integer)

        With Me
            .listPage.Height = .Height - 114

            Select Case i

                Case 0 'Catalog
                    .gbPackageConfig.Visible = True
                    .gbGeneral.Visible = False

                    .gbPackageConfig.Left = .listPage.Left + .listPage.Width + 10
                    .gbPackageConfig.Top = .listPage.Top
                    .gbPackageConfig.Width = .Width - (.listPage.Left + .listPage.Width + 40)
                    .gbPackageConfig.Height = .listPage.Height

                Case 1 ' General
                    .gbPackageConfig.Visible = False
                    .gbGeneral.Visible = True
                   
                    .gbGeneral.Left = .listPage.Left + .listPage.Width + 10
                    .gbGeneral.Top = .listPage.Top
                    .gbGeneral.Width = .Width - (.listPage.Left + .listPage.Width + 40)
                    .gbGeneral.Height = .listPage.Height

            End Select

            FormResize(i)

        End With

    End Sub

    Private Sub LPExecutePackageTaskForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        bStarting = False

        With Me
            .Width = 504
            .Height = 300
            .txtServer.Focus()
        End With

    End Sub

    Private Sub txtServer_Leave(sender As Object, e As EventArgs) Handles txtServer.Leave

        Exit Sub

        ' STOP HERE ... FOR NOW...


        sc = New SSISCatalog(txtServer.Text)
        Dim sqlCon As SqlConnection = sc.connect
        sqlCon.Open()
        If sqlCon.State = ConnectionState.Open Then
            LoadListPage()
        End If
        If sqlCon.State <> ConnectionState.Closed Then
            sqlCon.Close()
        End If

    End Sub

    Private Sub btnDisplayCatNav_Click(sender As Object, e As EventArgs) Handles btnDisplayCatNav.Click

        Dim frm As New frmSSISCatalogNavigator
        frm.ShowDialog(Me)

        Dim sTmp As String = frm.sFullSSISPkgPath
        Dim sServerName As String = Strings.Left(sTmp, Strings.InStr(sTmp, ".") - 1)
        Dim iStart As Integer = Strings.InStr(sTmp, ".") + 1
        Dim iEnd As Integer = Strings.InStr(sTmp, "\")
        Dim iLen As Integer
        Dim sCatalogName As String
        Dim sFolderName As String
        Dim sProjectName As String
        Dim sPackageName As String

        If iEnd > iStart Then
            iLen = iEnd - iStart
            sCatalogName = Strings.Mid(sTmp, iStart, iLen)
            sTmp = Strings.Right(sTmp, Strings.Len(sTmp) - iEnd)

            iStart = 1
            iEnd = Strings.InStr(sTmp, "\")
            If iEnd > iStart Then
                iLen = iEnd - iStart
                sFolderName = Strings.Mid(sTmp, iStart, iLen)
                sTmp = Strings.Right(sTmp, Strings.Len(sTmp) - iEnd)

                iStart = 1
                iEnd = Strings.InStr(sTmp, "\")
                If iEnd > iStart Then
                    iLen = iEnd - iStart
                    sProjectName = Strings.Mid(sTmp, iStart, iLen)
                    sTmp = Strings.Right(sTmp, Strings.Len(sTmp) - iEnd)
                    sPackageName = sTmp
                End If
            End If
        End If


        With Me
            .txtServer.Text = sServerName
            .txtCatalog.Text = sCatalogName
            .txtFolder.Text = sFolderName
            .txtProject.Text = sProjectName
            .txtPackage.Text = sPackageName
        End With

    End Sub
End Class
